package nuce.myClass;

import java.awt.Color;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import nuce.bo.QuestionBO;
import nuce.bo.QuestionSet;
import nuce.entity.Question;

public class MyQuestion {

    private int countQuestion = 0;
    private QuestionSet q; //tap cau hoi gom 15 cau : 5 cau level 1, 5 cau level 2  va` 5 cau level 3
    private int[] AnsTrue; // vi tri cua cau hoi dung chua set gia tri; hom sau set gia tri cho no'
    private QuestionSet q1, q2, q3; // q1,2,3 = tap cac cau hoi level1 ,2,3

    public MyQuestion() {
        q = new QuestionSet();
        countQuestion = 0;
        q1 = new QuestionSet();
        q2 = new QuestionSet();
        q3 = new QuestionSet();
    }

    public int getCountQuestion() {
        return countQuestion;
    }

    public int[] getAnsTrue() {
        int[] temp;
        int n = q.getCount();
        temp = new int[n];
        for (int i = 0; i < n; i++) {
            temp[i] = AnsTrue[i];
        }
        return temp;
    }

    public void setCountQuestion(int countQuestion) {
        this.countQuestion = countQuestion;
    }

    public QuestionSet getQ1() {
        return q1;
    }

    public void setQ1(QuestionSet q1) {
        this.q1 = q1;
    }

    public QuestionSet getQ2() {
        return q2;
    }

    public void setQ2(QuestionSet q2) {
        this.q2 = q2;
    }

    public QuestionSet getQ3() {
        return q3;
    }

    public QuestionSet getQ() {
        return q;
    }

    public void setQ3(QuestionSet q3) {
        this.q3 = q3;
    }

    public void createQuestion() {  //dang gap van de o tham chieu

        q1.SelectByLevel(1);
        q2.SelectByLevel(2);
        q3.SelectByLevel(3);
// Do du lieu vao q (15 cau hoi )
        int i = 0;
        int indextRandom;
        int count1;  //cout1  = so luong cau hoi level1, cout2 level2...

        Random rd = new Random();
        for (i = 0; i < 5; i++) {
            count1 = q1.getCount();
            if (count1 > 0) {
                indextRandom = rd.nextInt(count1);
                q.add(q1.getItem(indextRandom));
                q1.remove(indextRandom);
                count1--;
            } else {
                q.add(q1.getItem(0));
            }
        }
        for (i = 0; i < 5; i++) {
            if (q2.getCount() > 0) {
                indextRandom = rd.nextInt(q2.getCount());
                q.add(q2.getItem(indextRandom));
                q2.remove(indextRandom);
            } else {
                q.add(q2.getItem(1));
            }
        }
        for (i = 0; i < 5; i++) {
            if (q3.getCount() > 0) {
                indextRandom = rd.nextInt(q3.getCount());
                q.add(q3.getItem(indextRandom));
                q3.remove(indextRandom);
            } else {
                q.add(q2.getItem(0));
            }
        }
        randomAnwer();
    }

    public void changeLabel(JLabel lb) {
        Border b = new LineBorder(Color.RED);
        lb.setBorder(b);
    }

    // Random dap an dung, dap an dung ko nam o dap an A nua!!!!
    public void randomAnwer() {//chua dung den
        int i, indexRandom = 1;
        int n = q.getCount();
        Random rd = new Random();

        AnsTrue = new int[n];
        QuestionBO qb = new QuestionBO();
        Question qtemp = new Question();

        for (i = 0; i < n; i++) {
            indexRandom = rd.nextInt(4);
            qtemp = q.getItem(i);
            qb.swap(qtemp, 0, indexRandom);
            q.updateItem(i, qtemp);  //da sua
            AnsTrue[i] = indexRandom;
        }
    }

    public void removeQuestion(JRadioButton[] arrRdadio, int index) { //index la dap an dung 0 = A, 1 = B
        int x = 0, y = 1;
        Random r = new Random();
        do {
            x = r.nextInt(4);
        } while (x == index);
        do {
            y = r.nextInt(4);
        } while (y == index || x == y);
        arrRdadio[x].setEnabled(false);
        arrRdadio[y].setEnabled(false);
    }

    public boolean checkAnswer(int[] ans, int index, JRadioButton[] rd) {
        int i, j, k;
        k = 0;
        for (i = 0; i < 4; i++) {
            if (rd[i].isSelected()) {
                k = i;
            }
        }
        if (ans[index] == k) {
            return true;
        } else {
            return false;
        }
    }

    public void call(int i, int[] ans) {
        int x, k = 0;
        String str = "";
        Random r = new Random();
        x = r.nextInt(100);
        if (x < 70) {
            switch (ans[i]) {
                case 0:
                    str = "A";
                    break;
                case 1:
                    str = "B";
                    break;
                case 2:
                    str = "C";
                    break;
                case 3:
                    str = "D";
                    break;
            }
            JOptionPane.showMessageDialog(null, "Nguoi than tra loi(do chinh xac la 70%): nen chon dap an: " + str);
        } else {
            if (ans[i] < 3) {
                k = ans[i] + 1;
            } else {
                k = ans[i] - 1;
            }
            switch (k) {
                case 0:
                    str = "A";
                    break;
                case 1:
                    str = "B";
                    break;
                case 2:
                    str = "C";
                    break;
                case 3:
                    str = "D";
                    break;
            }
            JOptionPane.showMessageDialog(null, "Nguoi than cua ban tra loi: nen chon dap an: " + str);
        }
    }

    public void helpUser(int index, int[] ans) {
        int[] a, b;
        a = new int[4];
        int i, k, temp, max, l;
        i = temp = 0;
        Random r = new Random();
        for (i = 0; i <3; i++) {
            a[i] = r.nextInt(100 - temp);
            temp += a[i];
        }
        a[i]=100-temp;
        
        max = a[0];
        l = 0;
        for (i = 0; i < 4; i++) {
            if (max < a[i]) {
                max = a[i];
                l = i;
            }
        }
        k = r.nextInt(100);
        if (k < 80) { //xac suat la 80% so dong se tra loi dung
            temp = a[l];
            a[l] = a[ans[index]];
            a[ans[index]] = temp;
        } else {
            if (l == ans[index] && l < 3) {
                l++;
                temp = a[l];
                a[l] = a[ans[index]];
                a[ans[index]] = temp;
            } else {
                if (l == ans[index] && l == 3) {
                    l--;
                    temp = a[l];
                    a[l] = a[ans[index]];
                    a[ans[index]] = temp;
                }
            }
        }
        String str = "";
        for (i = 0; i < 4; i++) {
            str += "co " + a[i] + "% nguoi chon phuong an " + i + "\n";
        }
        JOptionPane.showMessageDialog(null, str);

    }
}
